home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 2 / AACD 2.iso / AACD / Programming / fpc / compiler / tgen68k.pas < prev    next >
Pascal/Delphi Source File  |  1998-09-24  |  21KB  |  685 lines

  1. {
  2.     $Id: tgen68k.pas,v 1.1.1.1.2.4 1998/07/21 12:11:37 carl Exp $
  3.     Copyright (c) 1993-98 by Florian Klaempfl, Carl Eric Codere
  4.  
  5.     This unit handles the temporary variables stuff for m68k
  6.  
  7.     This program is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.  
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.  
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  ****************************************************************************
  22. }
  23. unit tgen68k;
  24.  
  25.   interface
  26.  
  27.     uses
  28.        cobjects,globals,tree,hcodegen,verbose,files,aasm
  29. {$ifdef m68k}
  30.        ,m68k
  31. {$endif}
  32.        ;
  33.  
  34.     type
  35.        tregisterset = set of tregister;
  36.        tpushed = array[R_D0..R_A6] of boolean;
  37.  
  38.     const
  39.        { D2 to D5 usable as scratch registers }
  40.        usablereg32 : byte = 4;
  41.        { A2 to A4 usable as address registers }
  42.        usableaddress: byte = 3;
  43.        { FP2 to FP7 usable as FPU registers   }
  44.        usablefloatreg : byte = 6;
  45.  
  46.     function getregister32 : tregister;
  47.     procedure ungetregister32(r : tregister);
  48.     { return a free 32-bit address register }
  49.     function getaddressreg: tregister;
  50.  
  51.     procedure ungetregister(r : tregister);
  52.  
  53.     procedure cleartempgen;
  54.  
  55.     { generates temporary variables }
  56.     procedure resettempgen;
  57.     procedure setfirsttemp(l : longint);
  58.     function gettempsize : longint;
  59.     function gettempofsize(size : longint) : longint;
  60.     procedure gettempofsizereference(l : longint;var ref : treference);
  61.     function istemp(const ref : treference) : boolean;
  62.     procedure ungetiftemp(const ref : treference);
  63.     function getfloatreg: tregister;
  64.     { returns a free floating point register }
  65.     { used in real, fpu mode, otherwise we   }
  66.     { must use standard register allocation  }
  67.  
  68.     procedure del_reference(const ref : treference);
  69.     procedure del_locref(const location : tlocation);
  70.  
  71.  
  72.     { pushs and restores registers }
  73.     procedure pushusedregisters(var pushed : tpushed;b : word);
  74.     procedure popusedregisters(const pushed : tpushed);
  75.  
  76.     var
  77.        unused,usableregs : tregisterset;
  78.        c_usableregs : longint;
  79.  
  80.        usedinproc : word;
  81.  
  82.        { count, how much a register must be pushed if it is used as register }
  83.        { variable                                                            }
  84.        reg_pushes : array[R_D0..R_A6] of longint;
  85.        is_reg_var : array[R_D0..R_A6] of boolean;
  86.  
  87.   implementation
  88.  
  89.  
  90.     function getusableaddr: byte;
  91.     { Since address registers are different then data registers }
  92.     { we check the unused register list to determine the number }
  93.     { of address registers which are available.                 }
  94.     var
  95.       i: byte;
  96.     Begin
  97.       i:=0;
  98.       if R_A2 in unused then
  99.         Inc(i);
  100.       if R_A3 in unused then
  101.         Inc(i);
  102.       if R_A4 in unused then
  103.          Inc(i);
  104.       getusableaddr:=i;
  105.     end;
  106.  
  107.     procedure pushusedregisters(var pushed : tpushed;b : word);
  108.  
  109.       var
  110.          r : tregister;
  111.  
  112.       begin
  113.          { the following registers can be pushed }
  114.          { D0, D1, D2, D3, D4, D5, D6, D7, A0    }
  115.          { A1, A2, A3, A4                        }
  116.          for r:=R_D2 to R_A4 do
  117.            begin
  118.               pushed[r]:=false;
  119.               { if the register is used by the calling subroutine    }
  120.               if ((b and ($800 shr word(r)))<>0) then
  121.                 begin
  122.                    { and is present in use }
  123.                    if not(r in unused) then
  124.                      begin
  125.                         { then save it }
  126.                         { then save it on the stack }
  127.                         exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,r,R_SPPUSH)));
  128.                         { here was a big problem  !!!!!}
  129.                         { you cannot do that for a register that is
  130.                         globally assigned to a var
  131.                         this also means that you must push it much more
  132.                         often, but there must be a better way
  133.                         maybe by putting the value back to the stack !! }
  134.                         if not(is_reg_var[r]) then
  135.                           unused:=unused+[r];
  136.                         pushed[r]:=true;
  137.                      end;
  138.                 end;
  139.            end;
  140.       end;
  141.  
  142.     procedure popusedregisters(const pushed : tpushed);
  143.  
  144.       var
  145.          r : tregister;
  146.  
  147.       begin
  148.          for r:=R_A4 downto R_D2 do
  149.            if pushed[r] then
  150.              begin
  151.                 exprasmlist^.concat(new(pai68k,op_reg_reg(A_MOVE,S_L,R_SPPULL,r)));
  152.                 unused:=unused-[r];
  153.              end;
  154.       end;
  155.  
  156.     procedure ungetregister(r : tregister);
  157.  
  158.       begin
  159.            ungetregister32(r)
  160.       end;
  161.  
  162.  
  163.     procedure del_reference(const ref : treference);
  164.  
  165.       begin
  166.          if ref.isintvalue then
  167.            exit;
  168.          ungetregister(ref.base);
  169.          ungetregister32(ref.index);
  170.       end;
  171.  
  172.     procedure del_locref(const location : tlocation);
  173.  
  174.       begin
  175.          if (location.loc<>loc_mem) and (location.loc<>loc_reference) then
  176.            exit;
  177.          if location.reference.isintvalue then
  178.            exit;
  179.          ungetregister(location.reference.base);
  180.          ungetregister32(location.reference.index);
  181.       end;
  182.  
  183.     procedure ungetregister32(r : tregister);
  184.  
  185.       begin
  186.          if r in [R_D2,R_D3,R_D4,R_D5,R_D7] then
  187.           begin
  188.              unused:=unused+[r];
  189.              inc(usablereg32);
  190.           end
  191.          else
  192.          if r in [R_FP2,R_FP3,R_FP4,R_FP5,R_FP6,R_FP7] then
  193.          begin
  194.               unused:=unused+[r];
  195.               inc(usablefloatreg);
  196.          end
  197.          else
  198.          if r in [R_A2,R_A3,R_A4] then
  199.            begin
  200.               unused:=unused+[r];
  201.               inc(usableaddress);
  202.            end;
  203.         { other registers are RESERVED and should not be freed }
  204.       end;
  205.  
  206.  
  207.     function getfloatreg: tregister;
  208.     { returns a free floating point register }
  209.     { used in real, fpu mode, otherwise we   }
  210.     { must use standard register allocation  }
  211.     var
  212.      i:tregister;
  213.     begin
  214.       dec(usablefloatreg);
  215.       if usablefloatreg = 0 then
  216.        Message(cg_f_internal_error_in_getfloatreg);
  217.       for i:=R_FP2 to R_FP7 do
  218.       begin
  219.          if i in unused then
  220.          begin
  221.            unused := unused-[i];
  222.            getfloatreg := i;
  223.            exit;
  224.          end;
  225.       end;
  226.       { if we are here, then there was an allocation failure }
  227.       Message(cg_f_internal_error_in_getfloatreg);
  228.     end;
  229.  
  230.  
  231.     function getaddressreg: tregister;
  232.  
  233.      begin
  234.          dec(usableaddress);
  235.          if R_A2 in unused then
  236.            begin
  237.               unused:=unused-[R_A2];
  238.               usedinproc:=usedinproc or ($800 shr word(R_A2));
  239.               getaddressreg:=R_A2;
  240.            end
  241.          else
  242.          if R_A3 in unused then
  243.            begin
  244.               unused:=unused-[R_A3];
  245.               usedinproc:=usedinproc or ($800 shr word(R_A3));
  246.               getaddressreg:=R_A3;
  247.            end
  248.          else
  249.          if R_A4 in unused then
  250.            begin
  251.               unused:=unused-[R_A4];
  252.               usedinproc:=usedinproc or ($800 shr word(R_A4));
  253.               getaddressreg:=R_A4;
  254.            end
  255.          else
  256.          begin
  257.            internalerror(10);
  258.          end;
  259.  
  260.      end;
  261.  
  262.     function getregister32 : tregister;
  263.       begin
  264.          dec(usablereg32);
  265.          if R_D2 in unused then
  266.            begin
  267.               unused:=unused-[R_D2];
  268.               usedinproc:=usedinproc or ($800 shr word(R_D2));
  269.               getregister32:=R_D2;
  270.            end
  271.          else if R_D3 in unused then
  272.            begin
  273.               unused:=unused-[R_D3];
  274.               usedinproc:=usedinproc or ($800 shr word(R_D3));
  275.               getregister32:=R_D3;
  276.            end
  277.          else if R_D4 in unused then
  278.            begin
  279.               unused:=unused-[R_D4];
  280.               usedinproc:=usedinproc or ($800 shr word(R_D4));
  281.               getregister32:=R_D4;
  282.            end
  283.          else if R_D5 in unused then
  284.            begin
  285.              unused:=unused-[R_D5];
  286.              usedinproc:=usedinproc or ($800 shr word(R_D5));
  287.              getregister32:=R_D5;
  288.            end
  289.          else if R_D7 in unused then
  290.            begin
  291.              unused:=unused-[R_D7];
  292.              usedinproc:=usedinproc or ($800 shr word(R_D7));
  293.              getregister32:=R_D7;
  294.            end
  295.          else
  296.          begin
  297.           internalerror(10);
  298.          end;
  299.       end;
  300.  
  301.     procedure cleartempgen;
  302.  
  303.       begin
  304.          unused:=usableregs;
  305.          usablereg32:=c_usableregs;
  306.          usableaddress:=getusableaddr;
  307.       end;
  308.  
  309.     type
  310.        pfreerecord = ^tfreerecord;
  311.  
  312.        tfreerecord = record
  313.           next : pfreerecord;
  314.           pos : longint;
  315.           size : longint;
  316. {$ifdef EXTDEBUG}
  317.           line : longint;
  318. {$endif}
  319.        end;
  320.  
  321.     var
  322.        tmpfreelist : pfreerecord;
  323.        templist : pfreerecord;
  324.        lastoccupied : longint;
  325.        firsttemp, maxtemp : longint;
  326.  
  327.     procedure resettempgen;
  328.  
  329.       var
  330.          hp : pfreerecord;
  331.  
  332.       begin
  333.          while assigned(tmpfreelist) do
  334.            begin
  335.               hp:=tmpfreelist;
  336.               tmpfreelist:=hp^.next;
  337.               dispose(hp);
  338.            end;
  339.          while assigned(templist) do
  340.            begin
  341. {$ifdef EXTDEBUG}
  342.               Comment(V_Warning,'temporary assignment of size '
  343.                        +tostr(templist^.size)+' from '+tostr(templist^.line)+
  344.                        +' at pos '+tostr(templist^.pos)+
  345.                        ' not freed at the end of the procedure');
  346. {$endif}
  347.               hp:=templist;
  348.               templist:=hp^.next;
  349. {$ifndef EXTDEBUG}
  350.               dispose(hp);
  351. {$endif not EXTDEBUG}
  352.            end;
  353.          templist:=nil;
  354.          tmpfreelist:=nil;
  355.          firsttemp:=0;
  356.          maxtemp:=0;
  357.          lastoccupied:=0;
  358.       end;
  359.  
  360.     procedure setfirsttemp(l : longint);
  361.  
  362.       begin
  363.          { this is a negative value normally }
  364.          if l < 0 then
  365.          Begin
  366.            if odd(l) then
  367.              Dec(l);
  368.          end
  369.          else
  370.          Begin
  371.            if odd(l) then
  372.             Inc(l);
  373.          end;
  374.          firsttemp:=l;
  375.          maxtemp := l;
  376.          lastoccupied:=l;
  377.       end;
  378.  
  379.     function gettempofsize(size : longint) : longint;
  380.  
  381.       var
  382.          last,hp : pfreerecord;
  383.  
  384.       begin
  385.          { this code comes from the heap management of FPC ... }
  386.          if (size mod 4)<>0 then
  387.            size:=size+(4-(size mod 4));
  388.            if assigned(tmpfreelist) then
  389.              begin
  390.                 last:=nil;
  391.                 hp:=tmpfreelist;
  392.                 while assigned(hp) do
  393.                   begin
  394.                      { first fit }
  395.                      if hp^.size>=size then
  396.                        begin
  397.                           gettempofsize:=hp^.pos;
  398.                           if hp^.pos-size < maxtemp then
  399.                             maxtemp := hp^.size-size;
  400.                           { the whole block is needed ? }
  401.                           if hp^.size>size then
  402.                             begin
  403.                                hp^.size:=hp^.size-size;
  404.                                hp^.pos:=hp^.pos-size;
  405.                             end
  406.                           else
  407.                             begin
  408.                                if assigned(last) then
  409.                                  last^.next:=hp^.next
  410.                                else
  411.                                  tmpfreelist:=nil;
  412.                                dispose(hp);
  413.                             end;
  414.                           exit;
  415.                        end;
  416.                      last:=hp;
  417.                      hp:=hp^.next;
  418.                   end;
  419.              end;
  420.           { nothing free is big enough : expand temp }
  421.           gettempofsize:=lastoccupied-size;
  422.           lastoccupied:=lastoccupied-size;
  423.           if lastoccupied < maxtemp then
  424.             maxtemp := lastoccupied;
  425.       end;
  426.  
  427.     function gettempsize : longint;
  428.  
  429.       begin
  430.          { we only push words and we want to stay on }
  431.          { even stack addresses                      }
  432.          { maxtemp is negative                       }
  433.          if (maxtemp mod 2)<>0 then
  434.            dec(maxtemp);
  435.          gettempsize:=-maxtemp;
  436.       end;
  437.  
  438.     procedure gettempofsizereference(l : longint;var ref : treference);
  439.  
  440.       var
  441.          tl : pfreerecord;
  442.  
  443.       begin
  444.          { do a reset, because the reference isn't used }
  445.          reset_reference(ref);
  446.          ref.offset:=gettempofsize(l);
  447.          ref.base:=procinfo.framepointer;
  448.          new(tl);
  449.          tl^.pos:=ref.offset;
  450.          tl^.size:=l;
  451.          tl^.next:=templist;
  452.          templist:=tl;
  453. {$ifdef EXTDEBUG}
  454.          tl^.line:=current_module^.current_inputfile^.line_no;
  455. {$endif}
  456.       end;
  457.  
  458.     function istemp(const ref : treference) : boolean;
  459.  
  460.       begin
  461.          istemp:=((ref.base=procinfo.framepointer) and
  462.            (ref.offset<firsttemp));
  463.       end;
  464.  
  465.     procedure ungettemp(pos : longint;size : longint);
  466.  
  467.       var
  468.          hp,newhp : pfreerecord;
  469.  
  470.       begin
  471.          if (size mod 4)<>0 then
  472.            size:=size+(4-(size mod 4));
  473.          if size = 0 then
  474.            exit;
  475.          if pos<=lastoccupied then
  476.            if pos=lastoccupied then
  477.              begin
  478.                 lastoccupied:=pos+size;
  479.                 hp:=tmpfreelist;
  480.                 newhp:=nil;
  481.                 while assigned(hp) do
  482.                   begin
  483.                      { conneting a free block }
  484.                      if hp^.pos=lastoccupied then
  485.                         begin
  486.                            if assigned(newhp) then newhp^.next:=nil
  487.                              else tmpfreelist:=nil;
  488.                            lastoccupied:=lastoccupied+hp^.size;
  489.                            dispose(hp);
  490.                            break;
  491.                         end;
  492.                      newhp:=hp;
  493.                      hp:=hp^.next;
  494.                   end;
  495.              end
  496.            else
  497.              begin
  498. {$ifdef EXTDEBUG}
  499.               Comment(V_Warning,'temp managment problem : ungettemp() pos < lastoccupied !');
  500. {$endif}
  501.              end
  502.          else
  503.            begin
  504.               new(newhp);
  505.               { size can be allways set }
  506.               newhp^.size:=size;
  507.               newhp^.pos := pos;
  508.               { if there is no free list }
  509.               if not assigned(tmpfreelist) then
  510.                 begin
  511.                    { then generate one }
  512.                    tmpfreelist:=newhp;
  513.                    newhp^.next:=nil;
  514.                    exit;
  515.                 end;
  516.               { search the position to insert }
  517.               hp:=tmpfreelist;
  518.               while assigned(hp) do
  519.                 begin
  520.                    { conneting two blocks ? }
  521.                    if hp^.pos+hp^.size=pos then
  522.                       begin
  523.                          inc(hp^.size,size);
  524.                          dispose(newhp);
  525.                          break;
  526.                       end
  527.                    { if the end is reached, then concat }
  528.                    else if hp^.next=nil then
  529.                      begin
  530.                         hp^.next:=newhp;
  531.                         newhp^.next:=nil;
  532.                         break;
  533.                      end
  534.                    { falls der n„chste Zeiger gr”áer ist, dann }
  535.                    { Einh„ngen                                 }
  536.                    else if hp^.next^.pos<=pos+size then
  537.                      begin
  538.                         { concat two blocks ? }
  539.                         if pos+size=hp^.next^.pos then
  540.                           begin
  541.                              newhp^.next:=hp^.next^.next;
  542.                              inc(newhp^.size,hp^.next^.size);
  543.                              dispose(hp^.next);
  544.                              hp^.next:=newhp;
  545.                           end
  546.                         else
  547.                           begin
  548.                              newhp^.next:=hp^.next;
  549.                              hp^.next:=newhp;
  550.                           end;
  551.                         break;
  552.                      end;
  553.                    hp:=hp^.next;
  554.                 end;
  555.            end;
  556.       end;
  557.  
  558.     procedure ungetiftemp(const ref : treference);
  559.  
  560.       var
  561.          tl,prev : pfreerecord;
  562.  
  563.       begin
  564.          if istemp(ref) then
  565.            begin
  566.               prev:=nil;
  567.               tl:=templist;
  568.               while assigned(tl) do
  569.                 begin
  570.                    if ref.offset=tl^.pos then
  571.                      begin
  572.                         ungettemp(ref.offset,tl^.size);
  573.                         if assigned(prev) then
  574.                           prev^.next:=tl^.next
  575.                         else
  576.                           templist:=tl^.next;
  577.                         dispose(tl);
  578.                         exit;
  579.                      end
  580.                    else
  581.                      begin
  582.                         prev:=tl;
  583.                         tl:=tl^.next;
  584.                      end;
  585.                 end;
  586. {$ifdef EXTDEBUG}
  587.               Comment(V_Warning,'Internal: temp managment problem : '+
  588.                 'temp not found for release at offset '+tostr(ref.offset));
  589. {$endIf}
  590.            end;
  591.       end;
  592.  
  593. begin
  594.    { contains both information on Address registers and data registers }
  595.    { even if they are allocated separately.                            }
  596.    usableregs:=[R_D0,R_D1,R_D2,R_D3,R_D4,R_D5,R_D6,R_D7,R_A0,R_A1,R_A2,R_A3,R_A4,
  597.                R_FP0,R_FP1,R_FP2,R_FP3,R_FP4,R_FP5,R_FP6,R_FP7];
  598.    c_usableregs:=4;
  599.    tmpfreelist:=nil;
  600.    templist:=nil;
  601. end.
  602. {
  603.   $Log: tgen68k.pas,v $
  604.   Revision 1.1.1.1.2.4  1998/07/21 12:11:37  carl
  605.     * cleartempgen , also clears the address register count
  606.     * ungetregister was not checking what type of register trying to free
  607.  
  608.   Revision 1.1.1.1  1998/03/25 11:18:15  root
  609.   * Restored version
  610.  
  611.   Revision 1.12  1998/03/22 12:45:38  florian
  612.     * changes of Carl-Eric to m68k target commit:
  613.       - wrong nodes because of the new string cg in intel, I had to create
  614.         this under m68k also ... had to work it out to fix potential alignment
  615.         problems --> this removes the crash of the m68k compiler.
  616.       - added absolute addressing in m68k assembler (required for Amiga startup)
  617.       - fixed alignment problems (because of byte return values, alignment
  618.         would not be always valid) -- is this ok if i change the offset if odd in
  619.         setfirsttemp ?? -- it seems ok...
  620.  
  621.   Revision 1.11  1998/03/10 04:21:15  carl
  622.     * fixed extdebug problems
  623.  
  624.   Revision 1.10  1998/03/10 01:17:30  peter
  625.     * all files have the same header
  626.     * messages are fully implemented, EXTDEBUG uses Comment()
  627.     + AG... files for the Assembler generation
  628.  
  629.   Revision 1.9  1998/03/06 00:53:00  peter
  630.     * replaced all old messages from errore.msg, only ExtDebug and some
  631.       Comment() calls are left
  632.     * fixed options.pas
  633.  
  634.   Revision 1.8  1998/03/02 01:49:35  peter
  635.     * renamed target_DOS to target_GO32V1
  636.     + new verbose system, merged old errors and verbose units into one new
  637.       verbose.pas, so errors.pas is obsolete
  638.  
  639.   Revision 1.7  1998/02/13 10:35:51  daniel
  640.   * Made Motorola version compilable.
  641.   * Fixed optimizer
  642.  
  643.   Revision 1.6  1998/01/11 03:40:16  carl
  644.     + added fpu register allocation
  645.  
  646.   Revision 1.3  1997/12/09 14:13:07  carl
  647.   * bugfix of free register list.
  648.  
  649.   Revision 1.2  1997/11/28 18:14:49  pierre
  650.    working version with several bug fixes
  651.  
  652.   Revision 1.1.1.1  1997/11/27 08:33:03  michael
  653.   FPC Compiler CVS start
  654.  
  655.   Pre-CVS log:
  656.  
  657.   + feature added
  658.   - removed
  659.   * bug fixed or changed
  660.  
  661.   History (started with version 0.9.0):
  662.        7th december 1996:
  663.          * some code from Pierre Muller inserted
  664.            makes the use of the stack more efficient
  665.    5th september 1997:
  666.         + Converted for Motorola MC68000 output (C. E. Codere)
  667.    24nd september 1997:
  668.         + Reserved register list modified. (CEC)
  669.    26 september 1997:
  670.         + Converted to work with v093 (CEC)
  671.         * Knowing that base is in address register, modified routines
  672.           accordingly. (CEC)
  673.    27 september 1997:
  674.       + pushusedregisters now pushes only non-scratch registers.
  675.     2nd october 1997:
  676.       + added strict error checking when extdebug defined.
  677.    23 october 1997:
  678.       - it seems that sp, and the base pointer can be freed in ungetregister,
  679.         removed warning accordingly. (CEC).
  680.       * bugfix of address register in usableregs set. (They were not defined...) (CEC).
  681.       * other stupid bug! When I changed the register conventions, I forgot to change
  682.         getaddressreg to reflect those changes!! (CEC).
  683.  
  684. }
  685.